# 读取Excel模块 pip install xlrd 现在只支持.xls
import xlrd
# 登录邮箱，发邮件
import smtplib
from email.mime.text import MIMEText  # 用来包装内容
from email.header import Header  # 包装头部信息


def main():
    # 读取文件
    data = xlrd.open_workbook('../data/名单.xls')
    # 获取sheet1页
    sheet = data.sheets()[0]

    for i in range(sheet.nrows):
        # print(sheet.row_values(i))
        # 获取i行第二列
        name = sheet.cell_value(i, 1)
        # 获取i行第三列
        old_money = sheet.cell_value(i, 2)
        # 获取i行第五列
        new_money = sheet.cell_value(i, 4)
        # 获取i行第六列
        address = sheet.cell_value(i, 5)
        print(name, old_money, new_money, address)
        # 发送邮件
        # send_email(address, name, old_moeny, new_moeny)


def send_email(address, name, old_money, new_money):
    # 发送者
    from_addr = '2310545976@qq.com'
    # 发给谁
    to_person = address
    server = smtplib.SMTP('smtp.qq.com', 25)
    server.login(from_addr, '13232322')

    # 主题  需要包装
    subject = Header('邮件主题', 'utf-8')
    sender = Header('百战程序员', 'utf-8')
    # 邮件内容
    msg = MIMEText(f'<h1>通知</h1>: 您好{name},由于<b>充值</b>，您的余额已经从{old_money}变为{new_money}', 'html',
                   'utf-8')
    msg['From'] = sender
    msg['Subject'] = subject
    server.sendmail(from_addr, to_person, msg.as_string())


if __name__ == '__main__':
    main()
